package com.kfm.service.impl;

import com.kfm.dao.MoneyDao;
import com.kfm.service.MoneyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(readOnly = true, propagation = Propagation.REQUIRED)
public class MoneyServiceImpl implements MoneyService {

    @Autowired
    private MoneyDao dao;


    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
    public void transfer(Integer sourceId, Integer targetId, Double money) {
        // TODO() 验证 转账人和接收人是否存在
        // TODO() 转账人余额是否足够
        // TODO() 转账金额是否合法

        // 扣钱
        int row = dao.updateMoney(sourceId, -money);

        if (row > 0){
            int a = 1 / 0;
            // 加钱
            dao.updateMoney(targetId, money);
        }
    }
}
